
Rem
	bbdoc: Reconfigurable Input type.
	about:
EndRem
Type TInput

	field _game:TGame
	field controls:TBag

	'true when user is reconfiguring controls
	Field configuring:Int
	'control at this index being reconfigured
	Field configuringIndex:Int


	Method New()
		controls = New TBag'TList
	EndMethod


	Method AddControl(c:TControl)
		controls.Add(c)
	EndMethod


	Method StartConfiguring()
		configuring = true
		configuringIndex=0
	EndMethod


	'updates all key controls
	'or scans for key when reconfiguring
	'key_escape exits this mode
	Method Update()
		if configuring

			'scan for key being hit
			For Local keyCode:Int = 0 To 200
				if keyhit(keyCode) = 1
					if keyCode = KEY_ESCAPE
						configuring = false
					else
						'set this code to current control
						TControl(controls.Get(configuringIndex)).key = keyCode

						configuringIndex:+1
						if configuringIndex = controls.GetSize()
							configuring = false
						endif
						exit
					endif
				endif
			Next
		Else
			'normal update
			For Local index:Int = 0 To controls.GetSize()-1
				TControl(controls.Get(index)).Update()
			Next
		Endif

	EndMethod


	Method GetDown:Int(controlName:String)
		return Self.GetControl(controlName).GetDown()
	EndMethod


	Method GetHit:Int (controlName:String)
		return Self.GetControl(controlName).GetHit()
	EndMethod


	'gets a control by name
	Method GetControl:TControl (controlName:String)
		For Local index:Int = 0 To controls.GetSize()-1
			Local c:TControl = TControl(controls.Get(index))
			if c.GetName() = controlName then return c
		Next
	EndMethod


	'reconfigure control
	Method SetControlKey(controlName:String, key:Int)
		For Local index:Int = 0 To controls.GetSize()-1
			Local c:TControl = TControl(controls.Get(index))
			if c.GetName() = controlName
				c.key = key
				return
			endif
		Next
	EndMethod


	'processes all controls and places settings in passed ini file
	Method ToIniFile(i:TINIFile)
		For Local index:Int = 0 To controls.GetSize()-1
			Local c:TControl = TControl(controls.Get(index))
			i.SetIntValue("Input", c.GetName(), c.key)
		Next
	EndMethod



	'this method is called by the game when reconfiguring
	'a control
	Method Render()
		TRenderState.Push()
		TRenderState.Reset()

		'black border
		SetColor(0,0,0)
		SetAlpha(0.95)
		DrawRect(0, _game.GetGameHeight()-25, _game.GetGameWidth(), 30)

		'draw text
		SetAlpha(1.0)
		SetColor(100,255,255)
		_game.RenderText("Select new key for control: " + TControl(controls.Get(configuringIndex)).GetName(), ..
			0, _game.GetGameHeight()-20, true, true )

		SetColor(255,255,255)
		_game.RenderText("[ESCAPE] to cancel", 0, _game.GetGameHeight()-10, true, true)

		TRenderState.Pop()
	EndMethod
EndType